﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class SolutionTreeViewNodeHelper
    {
        public static void SaveNodes(List<SolutionTreeViewNode> nodes)
        {
            if (nodes == null || nodes.Count == 0)
            {
                return;
            }
            foreach (var node in nodes)
            {
                if (node.NodeType == NodeType.Scene)
                {
                    if (node.Isdirty)
                    {
                        Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, node.ID);
                        Designtime.Metadata.LocalStorage.SaveSceneFile(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution.ID, scene);
                        node.Isdirty = false;
                    }
                }
                else if (node.NodeType == NodeType.Solution)
                {
                    Designtime.Metadata.LocalStorage.SaveSolutionFile(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution);
                    node.Isdirty = false;
                }
            }
        }

        public static ResourceType GetResourceTypeByExt(string Extension)
        {
            ResourceType ret = ResourceType.Other;
            switch (Extension.ToLower())
            {
                case "jpg":
                case "bmp":
                case "png":
                case "ico":
                    ret = ResourceType.Image;
                    break;
                case "js":
                    ret = ResourceType.Js;
                    break;
                case "txt":
                    ret = ResourceType.Txt;
                    break;
                case "xml":
                    ret = ResourceType.Xml;
                    break;
                case "gif":
                case "wmv":
                    ret = ResourceType.Video;
                    break;
                default:
                    ret = ResourceType.Other;
                    break;
            }
            return ret;
        }

        public static void SaveCurrentScene()
        {
            UIContext.Current.CurrentDesignView.CaptureThumbnailImmediate();
            Designtime.Metadata.LocalStorage.SaveSceneFile(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution.ID, UIContext.Current.CurrentScene);
            UIContext.Current.CurrentEditedNode.Isdirty = false;
        }

        public static bool IsCancelSaveOrUnSaveCurrentSceneOperation()
        {
            if (UIContext.Current.CurrentEditedNode != null && UIContext.Current.CurrentEditedNode.Isdirty)
            {
                MessageBoxResult result = MessageBox.Show("当前场景有更新,是否保存？", "提示", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Yes);
                if (result == MessageBoxResult.Yes)
                {
                    SaveCurrentScene();
                }
                else if (result == MessageBoxResult.Cancel)
                {
                    return true;
                }
            }
            return false;
        }

        public static SolutionTreeViewNode GetStaticDataSourceFolderNode()
        {
            foreach (var node in UIContext.Current.SolutionNode.Children)
            {
                if (node.ID == Designtime.Metadata.Solution.StaticDataFolderId)
                {
                    return node;
                }
            }
            return null;
        }

        //获取父节点的新增子节点
        public static List<Guid> GetNewChildIdList(SolutionTreeViewNode root)
        {
            List<Guid> childrenIdList = new List<Guid>();
            foreach (var node in root.Children)
            {
                if (node.NodeStatus == NodeStatus.Add)
                {
                    childrenIdList.Add(node.ID);
                }
                if (node.Children != null && node.Children.Count > 0)
                {
                    childrenIdList.AddRange(GetNewChildIdList(node));
                }
            }
            return childrenIdList;
        }
        //获取新增子节点的签出父节点
        public static List<Guid> GetNewChildParentdIdList(SolutionTreeViewNode child)
        {
            List<Guid> parentIdList = new List<Guid>();
            while (child != null && child.ParentID != Guid.Empty)
            {
                SolutionTreeViewNode parent = GetNodeById(child.ParentID);
                if (parent != null && (parent.NodeStatus == NodeStatus.CheckOut || parent.NodeStatus == NodeStatus.Add))
                {
                    parentIdList.Add(parent.ID);
                }
                child = parent;
            }

            return parentIdList;
        }

        public static SolutionTreeViewNode GetStaticDataSourceNode(Guid guid)
        {
            foreach (var node in UIContext.Current.SolutionNode.Children)
            {
                if (node.ID == Designtime.Metadata.Solution.StaticDataFolderId)
                {
                    if (node.Children != null && node.Children.Count > 0)
                    {
                        foreach (var staticDSNode in node.Children)
                        {
                            if (staticDSNode.ID == guid)
                            {
                                return staticDSNode;
                            }
                        }
                    }
                    return null;
                }
            }
            return null;
        }

        public static SolutionTreeViewNode GetNodeById(Guid guid)
        {
            SolutionTreeViewNode retNode = null;
            if (UIContext.Current.SolutionNode.ID == guid)
            {
                return UIContext.Current.SolutionNode;
            }
            foreach (var node in UIContext.Current.SolutionNode.Children)
            {
                if (node.ID == guid)
                {
                    retNode = node;
                    break;
                }
                if (node.NodeType == NodeType.Folder || node.NodeType == NodeType.Scene)
                {
                    retNode = GetNodeById(node.Children, guid);
                    if (retNode != null)
                    {
                        break;
                    }
                }
            }
            return retNode;
        }
        public static SolutionTreeViewNode GetNodeById(System.Collections.ObjectModel.ObservableCollection<SolutionTreeViewNode> nodes, Guid guid)
        {
            SolutionTreeViewNode retNode = null;
            if (nodes == null || nodes.Count == 0)
            {
                return retNode;
            }
            foreach (var node in nodes)
            {
                if (node.ID == guid)
                {
                    retNode = node;
                    break;
                }
                if (node.NodeType == NodeType.Folder)
                {
                    retNode = GetNodeById(node.Children, guid);
                    if (retNode != null)
                    {
                        break;
                    }
                }
            }
            return retNode;
        }

        public static bool IsStaticDataFolder(SolutionTreeViewNode node)
        {
            if (node != null && node.NodeType == NodeType.Folder && node.ID == Designtime.Metadata.Solution.StaticDataFolderId)
            {
                return true;
            }
            return false;
        }

        public static void RemoveNodeById(Guid guid)
        {
            foreach (var node in UIContext.Current.SolutionNode.Children)
            {
                if (node.ID == guid)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        UIContext.Current.SolutionNode.Children.Remove(node);
                    });
                    break;
                }
                if (node.NodeType == NodeType.Folder || node.NodeType == NodeType.Scene)
                {
                    SolutionTreeViewNode removeNode = GetNodeById(node.Children, guid);
                    if (removeNode != null)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            node.Children.Remove(removeNode);
                        });
                        break;
                    }
                }
            }
        }
    }
}
